//
//  PhotoLoadUtil.m
//  Buddy
//
//  Created by caopan on 7/7/14.
//  Copyright (c) 2014 Changyou. All rights reserved.
//

#import "SLPhotoLoadUtil.h"

@implementation SLPhotoLoadUtil


static SLPhotoLoadUtil * gLoadUtil = nil;
static SLPhotoLoadBlock  gLoadPhotoBlock = nil;
static UIViewController * gController = nil;
static SLPhotoActionSheetBlock  gPhotoActionSheetBlock = nil;



+ (void)showSelectAction:(UIViewController *)controller
                   block:(SLPhotoActionSheetBlock)block
{
    gPhotoActionSheetBlock = [block copy];
    gController = controller;
    gLoadUtil = [[SLPhotoLoadUtil alloc] init];
    UIActionSheet * actionSheet = [[UIActionSheet  alloc] initWithTitle:@"Please Select Photo"
                                                               delegate:gLoadUtil
                                                      cancelButtonTitle:@"Cancel"
                                                 destructiveButtonTitle:nil
                                                      otherButtonTitles:@"Photo Album",@"Camera",nil];
    [actionSheet showInView:[[UIApplication sharedApplication] keyWindow]];
}

+ (BOOL) loadPhoto:(SLPhotoLoadBlock)block
          isCamera:(BOOL)isCamera
            sender:(id)object
{
    if (object == nil) {
        return NO;
    }
    
    gLoadPhotoBlock = [block copy];
    
    if (([UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypePhotoLibrary] == NO
         && [UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypeCamera] == NO)){
        return NO;
    }
    
    UIImagePickerController *imagePickerController = [[UIImagePickerController alloc] init];
	
    if (isCamera && YES == [UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypeCamera]) {
        imagePickerController.sourceType = UIImagePickerControllerSourceTypeCamera;
    }else{
        imagePickerController.sourceType = UIImagePickerControllerSourceTypePhotoLibrary;
    }
    
    [[self class] styleChanged:imagePickerController];

    [imagePickerController setDelegate:gLoadUtil];
    
	[object presentViewController:imagePickerController
                         animated:YES
                       completion:nil];
	

    
	return YES;
}


+ (void)styleChanged:(UIImagePickerController *)picker
{
    if (IOS7_OR_LATER) {
        picker.navigationController.navigationBar.translucent = NO;
        picker.navigationBar.barStyle = UIBarStyleBlackOpaque;
        picker.navigationBar.barTintColor= RGBColor10Bit(11, 11, 11, 1);
    } else {
        [picker.navigationController.navigationBar setBackgroundImage:[[UIImage imageNamed:@"headbar_bg"] stretchableImageWithLeftCapWidth:7.5 topCapHeight:22] forBarMetrics:UIBarMetricsDefault];
    }
     picker.navigationBar.tintColor=RGBColor10Bit(11, 11, 11, 1);
}


- (void)imagePickerController:(UIImagePickerController *)picker
        didFinishPickingImage:(UIImage *)image
                  editingInfo:(NSDictionary *)editingInfo
{
    [picker dismissViewControllerAnimated:YES completion:^{
        if (gLoadPhotoBlock) {
            gLoadPhotoBlock(image,NO);
        }
        [self reallocStaticObject];
    }];
}


- (void)imagePickerControllerDidCancel:(UIImagePickerController *)picker
{
    [picker dismissViewControllerAnimated:YES completion:^{
     if (gLoadPhotoBlock) {
//        gLoadPhotoBlock(nil,YES);
     }
     [self reallocStaticObject];
    }];
}



- (void)actionSheet:(UIActionSheet *)actionSheet clickedButtonAtIndex:(NSInteger)buttonIndex
{
    if (buttonIndex != actionSheet.cancelButtonIndex) {
        
        if (buttonIndex == 0) {
            [[self class] loadPhoto:^(UIImage *image, BOOL isCancel) {
                if (gPhotoActionSheetBlock) {
                    gPhotoActionSheetBlock(image);
                }
            } isCamera:NO sender:gController];
        }
        if (buttonIndex == 1) {
            [[self class] loadPhoto:^(UIImage *image, BOOL isCancel) {
                if (gPhotoActionSheetBlock) {
                    gPhotoActionSheetBlock(image);
                }
            } isCamera:YES sender:gController];
        }
        
    }
}


- (void)reallocStaticObject
{
    gLoadPhotoBlock = nil;
    gLoadUtil = nil;
    gController = nil;
    gPhotoActionSheetBlock = nil;
}



@end
